package com.xxxx.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.exception.GlobalException;
import com.xxxx.seckill.mapper.UserMapper;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.UserService;
import com.xxxx.seckill.utils.CookieUtil;
import com.xxxx.seckill.utils.MD5Utils;
import com.xxxx.seckill.utils.UUIDUtils;
import com.xxxx.seckill.vo.LoginVo;
import com.xxxx.seckill.vo.RespBean;
import com.xxxx.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:王亚栋
 * @Date:2023/2/12 17:15
 * @Version:1.0
 */
@Service
@Slf4j(topic = "UserServiceImpl")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {

        log.info("登录实体:{}",loginVo);

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

//        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtils.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        User user =  userMapper.selectById(mobile);

        if (user == null){

            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        //判断密码是否正确
        if (!MD5Utils.fromPassToDBPass(password,user.getSalt()).equals(user.getPassword())){

            throw new GlobalException(RespBeanEnum.PASSWORD_ERROR);
        }

        //生成UUID
        String ticket = UUIDUtils.uuid();

        //将用户信息存入Redis
        redisTemplate.opsForValue().set("user:" + ticket,user);

        //request.getSession().setAttribute(ticket,user);

        CookieUtil.setCookie(request,response,"userTicket",ticket);

        System.out.println(" CookieUtils.setCookie cookieName userTicket");

        return RespBean.success();
    }

    @Override
    public User getUserByCookie(String userTicket,
                                HttpServletRequest request,
                                HttpServletResponse response) {

        System.out.println("userTicket:" + userTicket);

        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);

        if (user != null) {

            try {
                CookieUtil.setCookie(request,response,"userTicket",userTicket);
            } catch (NullPointerException e) {
                e.printStackTrace();
                System.out.println("这里空指针异常");
            }

        }

        System.out.println("user= " + user);
        return user;
    }
}
